package cn.craftyun.yunexam.interceptor;

import cn.craftyun.yunexam.entity.User;
import cn.craftyun.yunexam.service.UserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登录拦截器
 */
@Log4j2
@Component
public class LoginUser implements HandlerInterceptor {
    @Resource
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        User examUser = userService.getLoginUser();
        if (examUser == null) {
            response.sendError(401);
            return false;
        }
        return true;
    }
}
